<template>
  <div>
    <el-dialog
      :title="obj.isAdd == true ? '菜单添加' : '菜单编辑'"
      :visible.sync="obj.isTrue"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select
            v-model="form.pid"
            placeholder="请选择上级菜单"
            @change="changepid()"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型">
          <el-radio v-model="form.type" :label="1" disabled>目录</el-radio>
          <el-radio v-model="form.type" :label="2" disabled>菜单</el-radio>
          {{ form.type }}
        </el-form-item>

        <el-form-item label="菜单图标" v-show="form.type == 1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              v-for="(item, index) in iconarr"
              :key="index"
              :value="item"
            >
              <i>{{ item }}</i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" v-show="form.type == 2">
          <el-select v-model="form.url" placeholder="请选择菜单地址">
            <el-option
              v-for="(item, index) in secondRouter"
              :key="index"
              :value="'/' + item.path"
              :label="item.meta.title"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button @click="submit()" v-if="obj.isAdd == true">添 加</el-button>
        <el-button type="primary" v-else @click="dits()">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { secondRouter } from "../../../router/index.js";
import { successAlert, warningAlert } from "../../../util/alter.js";
import { menuadd, menuInfo, menuDit, menuList } from "../../../requster/api.js";
import { mapGetters, mapActions } from "vuex";
export default {
  //接收父组件传来的参数
  props: ["obj"],
  data() {
    return {
      //菜单地址
      secondRouter: secondRouter,

      form: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      iconarr: [
        "el-icon-eleme",
        "el-icon-platform-eleme",
        "el-icon-info",
        "el-icon-s-release",
      ],
    };
  },
  computed: {
    ...mapGetters({
      menuList: "menu/list",
    }),
  },
  methods: {
    ...mapActions({
      reqmenuList: "menu/reqChangeList"
      }),
      changepid() {
        console.log(this.form.pid);
        if (this.form.pid == 0) {
          this.form.type = 1;

          this.form.url = "";
        } else {
          this.form.type = 2;
          this.form.icon = "";
        }
        console.log(this.form.type);
      },
    

    empty() {
      //清空封装
      this.form = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    cancel() {
      //取消事件
      //改变父组件的传来的isTure
      this.obj.isTrue = false;

      this.empty();
    },
    submit() {
      //点击添加按钮
      menuadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqmenuList();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条数据
    getone(id) {
      menuInfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          // 给form加id 确定编辑时没有id
          this.form.id = id;
        } else {
        }
      });
    },
    dits() {
      menuDit(this.form).then((res) => {
        console.log(this.form);
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqmenuList();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  
};
</script>

<style>
</style>